<template>
  <div class="goods">
    <el-button type="success" class="addBtn" @click="showAddDialog" plain>添加商品</el-button>
    <!-- @click="showGoodsDailog" -->
    <!-- 商品列表 -->
    <el-table :data="goodsList"  >
    <el-table-column  label="#" type="index"></el-table-column>
    <el-table-column prop="goods_name" label="商品名称"></el-table-column>
    <el-table-column prop="goods_price" label="商品价格"></el-table-column>
    <el-table-column prop="goods_weight" label="商品重量"></el-table-column>
    <el-table-column  label="创建时间">
      <template v-slot:default="{row}">
        {{ row.add_time | time}}
      </template>
    </el-table-column>
    <el-table-column  label="操作">
      <template v-slot:default='{ row }'>
           <el-button type="primary" size="small" plain  icon="el-icon-edit"></el-button>
           <el-button type="danger"  size="small" plain icon="el-icon-delete"></el-button>
           </template>
    </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page-sizes="[5, 10, 15, 20,30]"
      :page-size="pagesize"
      :current-page="pagenum"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
</template>
<script>
export default {
  created () {
    this.getgoodsList()
  },
  data () {
    return {
      // 商品列表
      goodsList: [],
      Pnp: '',
      pagenum: 1,
      pagesize: 5,
      total: 0
    }
  },
  methods: {
    //  渲染页面
    async  getgoodsList () {
      const { meta, data } = await this.$axios.get('goods', {
        params: {
          Pnp: this.Pnp,
          pagenum: this.pagenum,
          pagesize: this.pagesize
        }
      })
      if (meta.status === 200) {
        this.$message.success(meta.msg)

        this.pagenum = +data.pagenum
        this.total = data.total
        this.goodsList = data.goods
      } else {
        this.$message.error(meta.msg)
      }
    },
    // 处理切换页码大小时候触发的事件
    handleSizeChange (val) {
      // console.log(`每页 ${val} 条`)
      this.pagesize = val
      this.pagenum = 1
      this.getgoodsList()
    },
    // 当前页变动时候触发的事件
    handleCurrentChange (val) {
      // console.log(`当前页: ${val}`)
      this.pagenum = val
      this.getgoodsList()
    },
    showAddDialog () {
      this.$router.push('/goods-add')
    }
  }

}
</script>

<style lang='scss'>
.goods{
.addBtn{
margin-bottom: 10px;
}
}

</style>
